package com.example.spzx.controller;

import com.example.spzx.Service.SysUserService;
import com.example.spzx.Service.ValidateCodeService;
import com.example.spzx.model.dto.system.LoginDto;
import com.example.spzx.model.entity.system.SysUser;
import com.example.spzx.model.vo.common.Result;
import com.example.spzx.model.vo.common.ResultCodeEnum;
import com.example.spzx.model.vo.system.LoginVo;
import com.example.spzx.model.vo.system.ValidateCodeVo;
import com.example.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/index")
public class IndexController {
@Resource
private SysUserService sysUserService;
@Resource
private ValidateCodeService validateCodeService;
    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto, HttpSession httpSession){
        LoginVo loginVo=sysUserService.login(loginDto);
        httpSession.setAttribute("login",loginVo);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo=validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/getuserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader String token){
        SysUser sysUser=sysUserService.getUserInfo(token);
        System.out.println(sysUser);
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);

//        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);

    }
    @GetMapping("/logout")
    public Result<SysUser> logout(@RequestHeader String token,HttpSession httpSession){
        sysUserService.logout(token);
        httpSession.removeAttribute("login");
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
